//
//  QiuBaiCell.m
//  guess
//
//  Created by apple on 15/11/5.
//  Copyright © 2015年 fank. All rights reserved.
//

#import "QiuBaiCell.h"
#import "Define.h"
@interface QiuBaiCell()
{
    UIImageView *_headImageView;
    UILabel *_autherNameLab;
    UILabel *_titleLab;
    UIImageView *_posterImageView;
    UILabel *_sourceTitleLab;
    CGRect _frame;
    UILabel *_hotLab;
    UIView *_lineView;
    MPMoviePlayerController *_moviePlayer;
    UIImageView *_playImageView;
}

@end
@implementation QiuBaiCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)dealloc
{
    
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _frame = kscreenSize;
        //
        _moviePlayer.view.backgroundColor = [UIColor blackColor];
        _headImageView = [[UIImageView alloc]initWithFrame: CGRectMake(15, 15, 35, 35)];
        _headImageView.layer.cornerRadius = 35/2;
        _headImageView.layer.masksToBounds = YES;
        //
        _autherNameLab = [[UILabel alloc]initWithFrame:CGRectMake(65, 22.5, 200, 20)];
        _autherNameLab.textColor = [UIColor grayColor];
        //
        _titleLab = [[UILabel alloc]initWithFrame:CGRectZero];
        //
        _posterImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
        _posterImageView.userInteractionEnabled = YES;
        [_posterImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)]];
        //
        _shareBtn = [[UIButton alloc]initWithFrame:CGRectZero];
        
        [_shareBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_shareBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        [_shareBtn setBackgroundImage:[UIImage imageNamed:@"share.jpg"] forState:UIControlStateNormal];
        [_shareBtn setBackgroundImage:[UIImage imageNamed:@"share.jpg"] forState:UIControlStateHighlighted];
       

        //
        _sourceTitleLab = [[UILabel alloc]initWithFrame:CGRectZero];
        _sourceTitleLab.font = [UIFont systemFontOfSize:14];
        _sourceTitleLab.textColor = [UIColor lightGrayColor];
        _sourceTitleLab.text = @"视频来自糗事百科";
        //
        _hotLab = [[UILabel alloc]initWithFrame:CGRectMake(_frame.size.width-55, 25, 40, 15)];
        _hotLab.font = [UIFont systemFontOfSize:12];
        _hotLab.textColor = [UIColor lightGrayColor];
        _hotLab.text = @"热门";
        //
        _lineView = [[UIView alloc]initWithFrame:CGRectZero];
         _lineView.backgroundColor = [UIColor grayColor];
        //
        _playImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
        _playImageView.image = [UIImage imageNamed:@"play"];
        _playImageView.userInteractionEnabled = YES;
        [_playImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)]];
        
        [self addSubview:_headImageView];
        [self addSubview:_autherNameLab];
        [self addSubview:_titleLab];
        [self addSubview:_posterImageView];
        [self addSubview:_shareBtn];
        [self addSubview:_sourceTitleLab];
        [self addSubview:_hotLab];
        [self addSubview:_lineView];
        [_posterImageView addSubview:_playImageView];
       // [_posterImageView addSubview:_moviePlayer.view];
    }
    return self;
}
-(void)layoutSubviews
{
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:_headImageUrl]];

    _autherNameLab.text = _autherName;
    _shareBtn.tag = self.tag;
    CGSize labelSize = {0, 0};
    labelSize = [_title sizeWithFont:[UIFont systemFontOfSize:18]
                     constrainedToSize:CGSizeMake(_frame.size.width - 30, 5000)
                         lineBreakMode:NSLineBreakByWordWrapping];
    _titleLab.frame = CGRectMake(15, 65, _frame.size.width - 30, labelSize.height);
    _titleLab.numberOfLines = 0;
    _titleLab.text = _title;
    
    _posterImageView.frame = CGRectMake(15, 80 + labelSize.height, _frame.size.width-30, _frame.size.width-30);
    [_posterImageView sd_setImageWithURL:[NSURL URLWithString:_poster]];
    
    _playImageView.frame = CGRectMake((_frame.size.width-130)/2, (_frame.size.width-130)/2, 100, 100);
    
    _sourceTitleLab.frame = CGRectMake(15, _frame.size.width + 55 + labelSize.height, 200, 20);
    
    _shareBtn.frame = CGRectMake(_frame.size.width - 54.5, _frame.size.width + 52.5 + labelSize.height, 40, 40);
     _shareBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 7.5, 15, 7.5);
//    _shareBtn.backgroundColor = [UIColor redColor];
    
    _lineView.frame = CGRectMake(0, _frame.size.width + 95 + labelSize.height-1, _frame.size.width, 1);
}

- (void)prepareForReuse

{
    [super prepareForReuse];
    [self deletePlayer];
}

//-(void)shareBtn:(UIButton *)sender
//{
//    if (self.shareBlock) {
//        self.shareBlock(self.tag);
//    }
//}

-(void)tap
{
    NSLog(@"%ld",(long)self.tag);
    [self createMPPlayerController:_videoUrl];
}

- (void)createMPPlayerController:(NSString *)sFileNamePath {
    NSURL *movieURL = [NSURL URLWithString:sFileNamePath];
    if (!_moviePlayer) {
          _moviePlayer =[[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    }
    [_moviePlayer prepareToPlay];
    [_posterImageView addSubview:_moviePlayer.view];//设置写在添加之后   // 这里是addSubView
    _moviePlayer.shouldAutoplay=YES;
    [_moviePlayer setControlStyle:MPMovieControlStyleDefault];
    [_moviePlayer setFullscreen:NO];
    [_moviePlayer.view setFrame:_posterImageView.bounds];
    [_moviePlayer play];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(deletePlayer)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:_moviePlayer]; //播放完后的通知
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playStatus:)
                                                 name:MPMoviePlayerLoadStateDidChangeNotification
                                               object:_moviePlayer]; //播放完后的通知
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playStatus:)
                                                 name:MPMoviePlayerNowPlayingMovieDidChangeNotification
                                               object:_moviePlayer]; //播放完后的通知
    
}

-(void)deletePlayer
{
    [_moviePlayer stop];
    [_moviePlayer.view removeFromSuperview];
    _moviePlayer = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:_moviePlayer];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerNowPlayingMovieDidChangeNotification object:_moviePlayer];
}

-(void)playStatus:(NSNotification *)notification
{
    switch (_moviePlayer.loadState) {
        case MPMovieLoadStatePlaythroughOK:
            // 状态为缓冲几乎完成，可以连续播放
            [DejalActivityView activityViewForView:_moviePlayer.view];
            break;
        case MPMovieLoadStateStalled:
            // 缓冲中
            [DejalActivityView activityViewForView:_moviePlayer.view];
            break;
        default:
            break;
    }
    switch (_moviePlayer.playbackState) {
        case MPMoviePlaybackStatePlaying:
            // 可播放
            [DejalActivityView removeView];
            break;
        default:
            break;
    }
}
@end
